package com.atguigui.leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * 1790.仅执行一次字符串交换能否使两个字符串相等
 * Project: leetcode
 * Package: com.atguigui.leetcode
 * Version: 1.0
 * <p>
 * Created by WJX on 2022/10/11 9:23
 */
public class P1790CheckIfOneStringSwapCanMakeStringsEqual {

    public static void main(String[] args) {
        Solution solution = new P1790CheckIfOneStringSwapCanMakeStringsEqual().new Solution();
        // TO TEST
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public boolean areAlmostEqual(String s1, String s2) {
            if (s1.equals(s2)) {
                return true;
            }
            if (s1.length() != s2.length()) {
                return false;
            }
            List<Integer> diff = new ArrayList<Integer>();
            for (int i = 0; i < s1.length(); i++) {
                if (s1.charAt(i) != s2.charAt(i)) {
                    if (diff.size() >= 2) {
                        return false;
                    }
                    diff.add(i);
                }
            }

            if (diff.isEmpty()) {
                return true;
            }
            if (diff.size() != 2) {
                return false;
            }


            return s1.charAt(diff.get(0)) == s2.charAt(diff.get(1)) && s1.charAt(diff.get(1)) == s2.charAt(diff.get(0));

        }
    }
}
